/* eslint-disable */
/**
 * 打印相关工具类
 */
//ISO 216纸张尺寸（单位：mm）
const paperSizeMap = {
  //A系列格式
  A0: {width:841, height:1189},
  A1: {width:594, height:840},
  A2: {width:420, height:594},
  A3: {width:297, height:420},
  A4: {width:210, height:297},
  A5: {width:148, height:210},
  A6: {width:105, height:148},
  A7: {width:74, height:105},
  A8: {width:52, height:74},
  A9: {width:37, height:52},
  A10: {width:26, height:37},

  //B系列格式
  B0: {width:1000, height:1414},
  B1: {width:706, height:1000},
  B2: {width:500, height:707},
  B3: {width:353, height:500},
  B4: {width:250, height:353},
  B5: {width:176, height:250},
  B6: {width:125, height:176},
  B7: {width:88, height:125},
  B8: {width:62, height:88},
  B9: {width:44, height:62},
  B10: {width:31, height:44},

  //C系列格式
  C0: {width:917, height:1297},
  C1: {width:648, height:917},
  C2: {width:458, height:648},
  C3: {width:324, height:458},
  C4: {width:229, height:324},
  C5: {width:162, height:229},
  C6: {width:114, height:162},
  C7: {width:81, height:114},
  C8: {width:57, height:81},
  C9: {width:40, height:57},
  C10: {width:28, height:40 },

  //JIS
  B0JIS: {width:1030, height:1456},
  B1JIS: {width:728, height:1030},
  B2JIS: {width:515, height:728},
  B3JIS: {width:364, height:515},
  B4JIS: {width:257, height:364},
  B5JIS: {width:182, height:257},
  B6JIS: {width:128, height:182},
  B7JIS: {width:91, height:128},
  B8JIS: {width:64, height:91},
  B9JIS: {width:45, height:64},


}

const utilPrint = {
  paperSizeMap: paperSizeMap,
  /**pdf预览
   * @param   title 标题
   * @param   src  地址
   * @param   pageSize  纸张大小 A4  A5
   * @param   direction  纸张方向 transverse 横向   longitudinal纵向
   * @param   spacingMode  间距模式 normal正常间距  no_spacing 无间距
   * @param   perRowsOfGrid  每页数量 默认为20
   */
  openPdfView(
    title,
    src,
    pageSize,
    direction,
    spacingMode,
    perRowsOfGrid
  ){
    let home = global.vue.$children[0].$children[0]
    home.printFormShow({
      title: title,
      pageSize: pageSize,
      direction: direction,
      spacingMode: spacingMode,
      perRowsOfGrid: perRowsOfGrid,
      src: src,
    })
    // home.$refs.printForm.show({
    //   title: title,
    //   pageSize: pageSize,
    //   direction: direction,
    //   spacingMode: spacingMode,
    //   perRowsOfGrid: perRowsOfGrid,
    //   src: src,
    // })
  },

  /**检查打印客户端依赖依赖
   */
  isNeedCLodopfuncs(printServer, call, that){
    global.utilVue.loadShow(true, null,'正在下载服务文件')
    var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
    var oscript = document.createElement("script");

    if (!printServer) printServer = "localhost"
    oscript.src ="http://"+printServer+":8000/CLodopfuncs.js?priority=1";
    try {
      let isLoad = false
      setTimeout(() => {
        if (!isLoad) {
          global,utilMessage.warning(`打印客户端无法访问，客户端IP为${printServer}`)
          console.log(`打印客户端无法访问，客户端IP为${printServer}`)
        }
        global.utilVue.loadHide()
      }, 2000)

      head.insertBefore( oscript,head.firstChild );
      //引用双端口(8000和18000）避免其中某个被占用：
      oscript = document.createElement("script");
      oscript.src ="http://"+printServer+":18000/CLodopfuncs.js?priority=0";

      if(oscript.readyState){   //IE
        oscript.onreadystatechange=function(){
          if(script.readyState=='complete'||script.readyState=='loaded'){
            isLoad = true
            // global.utilMessage.success('服务文件下载完成')
            global.utilVue.loadHide()
            if (call) call(that)
          }
        }
      }else{    //非IE
        oscript.onload=function(){
          isLoad = true
          // global.utilMessage.success('服务文件下载完成')
          global.utilVue.loadHide()
          if (call) call(that)
        }
      }
      head.insertBefore( oscript,head.firstChild );
    }catch (e) {
      console.error('e', e)
    }
  }
}

export default utilPrint
